#pragma once

#include <QComboBox>
#include <string>
#include <unordered_map>
#include "layerMessage.h"
#include "qlib/all.h"
#include "toolsMessage.h"

#include <QImage>
#include "qlib/geo/key_point.h"

namespace qbe::UiMessage {
struct UpdateFileLisetFromLibrary {
  qlib::Library::s_ptr lib;
};
struct updateLibTreeFromMem {
  std::string mode_name;
  std::string pro_name;
  std::string lib_name;
};

struct SelectInfoMessage {
  QString str;
};
struct OperatorInfoMessage {
  QString str;
};
struct LayoutAdaptMessage {};
struct AddOperatorMessage {
  std::vector<std::any> add_graphs;
  std::vector<entt::entity> delete_graphs;
  std::vector<std::shared_ptr<qlib::Cell>> new_cells;
};

struct DisplayMouseCoorMessage {
  double x, y;
};

struct SwitchBoardUpdate {};

struct CancelDrawing {};
struct CompleteDrawing {};

struct ArrayUi_Open {
  std::vector<qlib::Vec2d> max_box;
};
struct ArrayUi_Ok {
  int row;
  int col;
  double row_x;
  double row_y;
  double col_x;
  double col_y;
};
struct ArrayUi_Close {};
struct ArrayUi_Cancel {};

struct SaveAsCellUi_Open {};
struct SaveAsCellUi_Ok {
  qlib::Library::s_ptr lib;  // 将要另存为的lib
  std::string name;
  bool is_layout_cell = true;
  bool is_replce_select_with_ref = false;
  std::string mode_name;
  std::string pro_name;
  std::string lib_name;
};
struct SaveAsCellUi_Close {};
struct SaveAsCellUi_Cancel {};

struct BreakUpUi_Open {};
struct BreakUpUi_Ok {
  bool is_break_up_all = false;
  bool is_reserve = true;
};
struct BreakUpUi_Close {};
struct BreakUpUi_Cancel {};

struct AddCellUi {
  std::vector<std::shared_ptr<qlib::Cell>> cellList;  // 文件列表调用
  bool isSameFile;

  qlib::Reference::s_ptr ref;  // 器件库调用
};

struct ShowCell_Mes {
  qlib::Cell::s_ptr cell;
};
struct HideCell_Mes {
  qlib::Cell::s_ptr cell;
};

struct ShowLayer_Mes {
  unsigned index;
};

struct FilletToolUi_Open {};

struct FilletToolUi_Ok {
  bool is_segment_mode = true;  // 折线模式 false折线模式 true线段模式
  bool is_trim = true;          // 是否修剪  true修剪 false不修剪
  double r = 5.0;               // 倒角半径
};
struct FilletToolUi_Close {};

struct UiEnableState {
  bool is_whole_selected_graph = false;
  bool is_part_selected_grpah = false;

  bool have_select_obj = false;  // 是否存在选中对象
  bool have_paste_obj = false;   // 是否存在可粘贴对象
  bool redo_enable = false;      // 是否可恢复
  bool undo_enable = false;      // 是否可撤销

  bool select_region_cutting_obj = false;  // 区域切割对应的特殊选中对象
  bool select_merge_obj = false;           // 合并对应的特殊选中对象

  bool select_any_two_obj = false;  // 选择任意两个或以上对象
  bool selct_boolean_obj = false;   // 选中布尔运算的两个或以上对象

  bool select_array_copy = false;  // 选中阵列复制所需条件

  bool select_one_obj = false;  // 选中仅一个对象
  bool select_one_ref = false;  // 选中仅一个引用
  bool in_lower_cell = false;   // 处于下层单元画板

  bool select_no3d_any_obj = false;   // 另存为单元条件
  bool lock_obj = false;              // 锁定对象条件
  bool select_least_one_obj = false;  // 选中至少一个引用对象
};

struct LabelUi_Open {};
struct LabelUi_Ok {
  std::string str;
};
struct LabelUi_Cancel {};

struct CreatePathUiOpen {};
struct CreatePathUiOk {
  double line_width = 0;
  double cente_line_width = 0;
  double radius_turning = 0;
};
struct CreatePathUiCancel {
  bool is_esc = false;
};

struct CreateLabelUiOpen {
  bool reset = false;
};
struct CreateLabelUiOk {
  std::string str;
  uint32_t layer;
  qlib::Anchor refer_point_pos;
  double rotation;
  double size;
};
struct CreateLabelUiCancel {};

struct CreateTextUiOpen {
  bool reset = false;
};
struct CreateTextUiOk {
  std::string str;
  uint32_t layer;
  qlib::Anchor refer_point_pos;
  double rotation;
  double size;
};
struct CreateTextUiCancel {};

struct CreatePinUiOpen {
  std::unordered_map<uint32_t, bool> id_use_map;
};
struct FlushPinMap {
  std::unordered_map<uint32_t, bool> id_use_map;
};
struct CreatePinUiOk {
  uint32_t id;
  std::string name;
  qlib::PinType type;
  double rotation;
};

struct CreatePinUiCancel {};

struct CreateKeyPointUiOpen {};
struct CreateKeyPointOk {
  std::string name;
  bool createLabel = false;
  double size;
  double rotation;
};
struct CreateKeyPointCancel {};

struct OpenManualRotationUi {};
struct ManualRotationOk {
  double rotation;
};
struct ManualRotationCancel {
  bool need_delete = false;
};

struct OpenAngleRotationUi {};

struct AngleRotationOk {
  bool self;
  double rotation;
};
struct AngleRotationCancel {};

struct OpenMoveUi {};
struct MoveOk {
  bool moveTo;
  double x;
  double y;
};

struct MoveCancel {};

struct OpenQuickAlignUi {
  bool isPoint;
};

struct QuickAlignOk {
  double x;
  double y;
};

struct QuickAlignCancel {};

struct OpenMirrorUi {
  bool isX;
  bool isOneObj;
};

struct MirrorOk {
  double dis;
  bool isSelf = false;
};

struct MirrorCancel {};
struct GetImage2D {
  qlib::Reference::s_ptr refer;
  int width;
  int height;
};

struct Shortcut_key {
  QString key;
};

struct Image2D {
  QImage image;
};
struct CurrentCellChanged {};

struct AttributePanel {};

struct UiSetting {
  struct Adsorption {
    QString name;
    bool adsorption;
  };
  QString drawing_board_background;                // 背景颜色
  QString drawing_board_grid_color;                // 格点颜色
  QString drawing_board_grid_type;                 // 格点类型
  bool drawing_board_lattice_adsorption;           // 格点吸附
  double drawing_board_adsorption_accuracy;        // 吸附精度
  bool drawing_board_fill;                         // 填充
  bool drawing_board_overlapping_color_change;     // 交叠变色
  QString select_color;                            // 选中颜色
  bool select_fill;                                // 选中填充
  QString preselected_colors;                      // 预选中颜色
  bool preselected_fill;                           // 预选中填充
  QString scale_color;                             // 比例尺颜色
  bool label_fixed_size;                           // 标签固定大小
  QString text_font;                               // 字体
  double pin_threshold;                            // 引脚显示阈值
  bool cursor_cross_cursor_display;                // 十字光标显示
  int cursor_line_type;                            // 线型
  int cursor_small_cursor;                         // 小光标
  int cursor_diaphaneity;                          // 透明度
  QString cursor_colour;                           // 十字光标颜色
  bool adsorption_enable_adsorption;               // 启用吸附
  std::vector<Adsorption> adsorption_adsorptions;  // 吸附
  int recognition_range;                           // 识别范围
};

struct BooleanOperation_Open {};
struct BooleanOperation_Ok {
  std::string operation_method;
  uint32_t output_layer;
  bool original_layer_retention = true;  // 原图层是否保留
  int truncated_state = 2;               // 0 截断 1 不截断 2 忽略
};
struct BooleanOperation_Close {};
struct BooleanOperation_ObjectState {
  bool show_operation_object = false;
  bool ok_enable = false;
  QString operation_object_info;
};
struct BooleanOperation_MethodIndexChanged {
  QString method_text;
};

struct Align_Open {
  bool layerRadioButtonState = false;
  std::vector<QComboBox*> comboboxs;
};
struct Align_Ok {
  bool is_apply = false;
  enum AlignMode {
    LeftAligned = 0,
    RightAligned,
    TopAligned,
    BottomAligned,
    HorizontallyCenterAligned,
    VerticallyCenterAligned,
  };
  AlignMode align_mode;
  int reference_standard = 0;  // 参考基准 0:select object 1:layer
  bool is_distance_open = true;
  double align_distance = 0;
};
struct Align_Close {};

struct OnlyRead {
  bool is_only_read = false;
};

struct FileListCellDelete {
  std::vector<qlib::Cell::s_ptr> delete_cells;
};

struct DriftUi_Open {};
struct DriftUi_Close {};
struct DriftUi_Ok  // 偏移
{
  double m_drift_distance = 10.000;
};
struct Drift_Cancle {};
struct SwitchCell_OP {
  qlib::Cell::s_ptr cell;
};
struct ReturnUpperCell_OP {};
struct ReturnTopCell_OP {};

struct SelectObjProperty {
  std::unordered_map<entt::entity, qlib::Polygon::s_ptr> polygons;
  std::unordered_map<entt::entity, qlib::Path::s_ptr> lines;
  std::unordered_map<entt::entity, qlib::Ellipse::s_ptr> ellipses;
  std::unordered_map<entt::entity, qlib::Fan::s_ptr> fans;
  std::unordered_map<entt::entity, qlib::Label::s_ptr> labels;
  std::unordered_map<entt::entity, qlib::Rectangle::s_ptr> rectangles;
  std::unordered_map<entt::entity, qlib::KeyPoint::s_ptr> key_points;
  std::unordered_map<entt::entity, qlib::Pin::s_ptr> pins;
  std::unordered_map<entt::entity, qlib::Ruler::s_ptr> rulers;
  std::unordered_map<entt::entity, qlib::Text::s_ptr> texts;
  std::unordered_map<entt::entity, qlib::Reference::s_ptr> references;
  std::unordered_map<entt::entity, qlib::SectionArea3D::s_ptr> section_area_3Ds;
  std::unordered_map<entt::entity, qlib::ViewArea3D::s_ptr> view_area_3Ds;

  void reset() {
    polygons.clear();
    lines.clear();
    ellipses.clear();
    fans.clear();
    labels.clear();
    rectangles.clear();
    rulers.clear();
    texts.clear();
    references.clear();
    key_points.clear();
    pins.clear();
    section_area_3Ds.clear();
    view_area_3Ds.clear();
  }
  bool empty() {
    if (polygons.empty() && lines.empty() && ellipses.empty() && fans.empty() && labels.empty() && rectangles.empty() && rulers.empty() &&
        texts.empty() && references.empty() && pins.empty() && key_points.empty() && section_area_3Ds.empty() && view_area_3Ds.empty())
      return true;
    return false;
  }
};

struct ChangeObjProperty {
  std::unordered_map<entt::entity, qlib::Polygon::s_ptr> polygons;
  std::unordered_map<entt::entity, qlib::Path::s_ptr> lines;
  std::unordered_map<entt::entity, qlib::Ellipse::s_ptr> ellipses;
  std::unordered_map<entt::entity, qlib::Fan::s_ptr> fans;
  std::unordered_map<entt::entity, qlib::Label::s_ptr> labels;
  std::unordered_map<entt::entity, qlib::Rectangle::s_ptr> rectangles;
  std::unordered_map<entt::entity, qlib::KeyPoint::s_ptr> key_points;
  std::unordered_map<entt::entity, qlib::Pin::s_ptr> pins;
  std::unordered_map<entt::entity, qlib::Ruler::s_ptr> rulers;
  std::unordered_map<entt::entity, qlib::Text::s_ptr> texts;
  std::unordered_map<entt::entity, qlib::Reference::s_ptr> references;
  std::vector<entt::entity> delete_objs;
  std::vector<entt::entity> uncheck;
  std::unordered_map<entt::entity, qlib::Polygon::s_ptr> path_topolygon;
  std::unordered_map<entt::entity, qlib::SectionArea3D::s_ptr> section_area_3Ds;
  std::unordered_map<entt::entity, qlib::ViewArea3D::s_ptr> view_area_3Ds;
};

struct AdsorptionAccuracyMessage {
  double acc;
};

struct DRCResult  //drc结果
{
  std::vector<std::vector<qlib::Vec2d>> paths;
  std::vector<std::vector<qlib::Vec2d>> polygons;
  std::vector<qlib::Vec2d> points;
  std::vector<qlib::Vec2d> bounding_box;
};
struct DRCCancelHighlight {};  //drc取消高亮

struct KeyEvent {
  std::string key;
  bool isPressed;
};

struct MouseRightMenu {
  bool open;
  qlib::Vec2d pos;
};

struct SelectJJPoint {};

}  // namespace qbe::UiMessage
